package com.evenmo.string;

import org.junit.Test;

public class StringTest {
    @Test
    public void String() {
        char[] value = {'a', 'b', 'c', 'd'};
        String s = new String(value);
        System.out.println(s);
    }

    /**
     * 用字符数组以x开始的n个字符创建一个String对象
     */
    @Test
    public void String2() {
        char[] value = {'a', 'b', 'c', 'd'};
        String s = new String(value, 1, 3);
        System.out.println(s.length());
        System.out.println(s);
    }

    /**
     * 获取字符串某一位置的字符
     */
    @Test
    public void getCharAt() {
        String s = "hello world !";
        char c = s.charAt(2);
        System.out.println(c);
    }

    /**
     * 获取子字符串
     */
    @Test
    public void getSubString() {
        String s = "hello world";
        String substring = s.substring(0);
        String substring1 = s.substring(0, 5);
        String s3 = s.substring(0, 4);
        String s4 = s.substring(0, s.length());//超出字符串长度会发生字符串下标越界异常
        System.out.println(substring);
        System.out.println(substring1);
        System.out.println(s3);
        System.out.println(s4);
    }

    @Test
    public void CompareString() {
        String s1 = "abc";
        String s2 = "abd";
        String s3 = "abc";
        int i = s1.compareTo(s2);
        int i1 = s2.compareTo(s1);
        int i2 = s1.compareTo(s3);
        System.out.println(i);
        System.out.println(i1);
        System.out.println(i2);

        //比较中文
//        s1="阿五二";
//        s2="李六一";
//        int i3 = s1.compareTo(s2);
//        System.out.println(i3);


    }

    @Test
    public void testToUpperCase() {
        String s = "this is my home!".toUpperCase();
        System.out.println(s);

    }

    @Test
    public void testToLowerCase() {
        String s = "THIS IS MY HOME!".toLowerCase();
        System.out.println(s);

    }

    @Test
    public void testTrim() {
        String s = " this is have trim's String ";
        System.out.println(s.length());
        String trim = s.trim();
        System.out.println(trim.length());
        System.out.println(trim);
    }

    @Test
    public void testSplit() {
        String s = "this is my first program!";
        System.out.println(s.length());
        String s3 = "a_b_c_";
        String[] s1 = s.split(" ", 4);
        System.out.println(s1.length);
        for (String s2 : s1) {
            System.out.println(s2);
        }
        String[] s2 = s3.split("_");
        System.out.println(s2.length);
        for (String s4 : s2) {
            System.out.println(s4);

        }

    }

    @Test
    public void testValueOf() {
        String s = String.valueOf(112.34f);

        System.out.println(s);
        System.out.println(String.valueOf(true));
    }

    /**
     * 替换单个字符
     */
    @Test
    public void testReplace() {
        String s = "this#is#my#home!";
        String replace = s.replace('#', ' ');
        System.out.println(replace);
    }

    @Test
    public void testReplaceFirst() {
        String s = "hello$World!$,this java's world!";
        String s1 = s.replaceFirst("$", " ");
        System.out.println(s1);

    }

    /**
     * 字符串替换
     */
    @Test
    public void testReplaceAll() {
        String s = "this is my home in Hang Zhou!";
        String s1 = s.replaceAll("Hang Zhou", "Bei Jing");
        System.out.println(s1);

    }

    @Test
    public void testGetBytes() {
        String s = "abcdefg...xyz!";
        s = "ZBCDEFG";
        byte[] bytes = s.getBytes();
        for (byte aByte : bytes) {
            System.out.println(aByte);
        }


    }

    @Test
    public void testToArray() {
        String s = "abcdefg...xyz!";
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            System.out.println(aChar);

        }

    }

    @Test
    public void testContains() {
        String s="hello some error code!";
        boolean some = s.contains("some");
        System.out.println(some);

    }
}
